﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace MvcUtil
{
    public class Securities
    {
        public static String hashPassword(String password)
        {
            Encoding enc = Encoding.ASCII;
            byte[] buffer = enc.GetBytes(password);
            SHA1CryptoServiceProvider cryptoTransformSHA1 =
            new SHA1CryptoServiceProvider();
            string hash = BitConverter.ToString(
                cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");

            return hash;
        }

        public static String getTransDate(DateTime date)
        {
            return String.Format("{0:MMddHHmmss}", date);
        }

        public static string ComputeHash(string input, HashAlgorithm algorithm)
        {
            Byte[] inputBytes = Encoding.UTF8.GetBytes(input);

            Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);

            return Encoding.UTF8.GetString(hashedBytes);
        }

        public static string ByteArrayToHexString(byte[] Bytes)
        {
            StringBuilder Result = new StringBuilder();
            string HexAlphabet = "0123456789ABCDEF";

            foreach (byte B in Bytes)
            {
                Result.Append(HexAlphabet[(int)(B >> 4)]);
                Result.Append(HexAlphabet[(int)(B & 0xF)]);
            }

            return Result.ToString();
        }

        public static byte[] HexStringToByteArray(string Hex)
        {
            byte[] Bytes = new byte[Hex.Length / 2];
            int[] HexValue = new int[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 
                0x06, 0x07, 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };

            for (int x = 0, i = 0; i < Hex.Length; i += 2, x += 1)
            {
                Bytes[x] = (byte)(HexValue[Char.ToUpper(Hex[i + 0]) - '0'] << 4 |
                                  HexValue[Char.ToUpper(Hex[i + 1]) - '0']);
            }

            return Bytes;
        }

        public static String base64Encode(String data)
        {
            if (data.Length == 0) return "";
            Byte[] encData_byte = new Byte[data.Length - 1];
            encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
            String encodedData = Convert.ToBase64String(encData_byte);
            return encodedData;
        }
        public static String base64Decode(String data)
        {
            UTF8Encoding encoder = new System.Text.UTF8Encoding();
            Decoder utf8Decode = encoder.GetDecoder();

            Byte[] todecode_byte = Convert.FromBase64String(data);
            int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
            Char[] decoded_char = new Char[charCount];
            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
            String result = new String(decoded_char);
            return result;
        }

        //Decrpyt mã thẻ

        public String Decrypt(String encryptedText, String key)
        {
            byte[] bKey = Encoding.ASCII.GetBytes(key);
            if (key.Length < 24) return null;
            byte[] data = Hex2Bin(encryptedText);
            byte[] dec = new byte[0];
            TripleDES tdes = TripleDES.Create("TripleDES");
            byte[] b = new byte[24];
            for (int i = 0; i < 24; i++)
            {
                b[i] = bKey[i];
            }
            tdes.Key = b;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;
            ICryptoTransform ict = tdes.CreateDecryptor();
            dec = ict.TransformFinalBlock(data, 0, data.Length);
            return Encoding.ASCII.GetString(dec);
        }

        private byte[] Hex2Bin(String hexvalue)
        {
            string s;
            int i = 0;
            byte[] result = new byte[hexvalue.Length / 2];

            while (hexvalue.Length > 0)
            {
                s = hexvalue.Substring(0, 2);
                int n = Convert.ToInt32(s, 16);
                byte c = (byte)n;
                result[i] = c;
                hexvalue = hexvalue.Substring(2);
                i++;
            }

            return result;
        }

        public static string EncryptMD5(string passWord)
        {
            MD5CryptoServiceProvider _md5Hasher = new MD5CryptoServiceProvider();
            byte[] bs = Encoding.UTF8.GetBytes(passWord);
            bs = _md5Hasher.ComputeHash(bs);
            StringBuilder s = new StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2"));
            }
            return s.ToString();
        }
    }
}
